﻿
Partial Class WebForm_ManageCodeMapping_ManageEcolifeImportMapping
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.pnlADD.GroupingText = Me.GetLocalResourceObject("pnlAddTopic")
            Me.pnlList.GroupingText = Me.GetLocalResourceObject("pnlListTopic")
            Me.BindDDLGroup(Me.ddlGroup)
            Me.BindDDLTableName(Me.ddlTableName, Me.ddlGroup.SelectedValue)
            Me.BindDDLValue(Me.ddlValue, Me.ddlTableName.SelectedValue)
            Me.BindGV()
        End If
    End Sub

#Region "頁面事件"
    Protected Sub ddlGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlGroup.SelectedIndexChanged
        Me.BindDDLTableName(Me.ddlTableName, Me.ddlGroup.SelectedValue)
        Me.BindDDLValue(Me.ddlValue, Me.ddlTableName.SelectedValue)
        Me.BindGV()
    End Sub

    Protected Sub ddlTableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTableName.SelectedIndexChanged
        Me.BindDDLValue(Me.ddlValue, Me.ddlTableName.SelectedValue)
        Me.BindGV()
    End Sub

    Protected Sub ddlValue_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlValue.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAdd.Click
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("Insert into EcolifeItemMapping(TableName,Value,ItemName) ")
        sql.AppendFormat("select '{0}' ", Me.ddlTableName.SelectedValue)
        sql.AppendFormat(",'{0}' ", Me.ddlValue.SelectedValue)
        sql.AppendFormat(",'{0}' ", Me.txtItemName.Text)
        dat.AccData(sql.ToString())
        Me.txtItemName.Text = ""
        Me.BindGV()
    End Sub
#End Region

#Region "GridView事件"
    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        '編輯
        Dim ddlUpdateGroup As DropDownList = e.Row.FindControl("ddlUpdateGroup")
        If ddlUpdateGroup IsNot Nothing Then

            Dim ddlUpdateTableName As DropDownList = e.Row.FindControl("ddlUpdateTableName")
            Dim ddlUpdateValue As DropDownList = e.Row.FindControl("ddlUpdateValue")

            Me.BindDDLGroup(ddlUpdateGroup)
            MyObj.MatchItem(ddlUpdateGroup, DataBinder.Eval(e.Row.DataItem, "Group_ID"))

            Me.BindDDLTableName(ddlUpdateTableName, ddlUpdateGroup.SelectedValue)
            MyObj.MatchItem(ddlUpdateTableName, DataBinder.Eval(e.Row.DataItem, "TableName"))

            Me.BindDDLValue(ddlUpdateValue, ddlUpdateTableName.SelectedValue)
            MyObj.MatchItem(ddlUpdateValue, DataBinder.Eval(e.Row.DataItem, "Value"))
        End If
    End Sub

    '編輯
    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub

    '取消編輯
    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub

    '分頁
    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    '排序
    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    '按鈕事件
    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        If e.CommandName = "ItemUpdate" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            Dim ddlUpdateTableName As DropDownList = row.FindControl("ddlUpdateTableName")
            Dim ddlUpdateValue As DropDownList = row.FindControl("ddlUpdateValue")
            Dim txtUpdateItemName As TextBox = row.FindControl("txtUpdateItemName")

            sql.AppendFormat("Update EcolifeItemMapping ")
            sql.AppendFormat("set TableName='{0}' ", ddlUpdateTableName.SelectedValue)
            sql.AppendFormat(",Value='{0}' ", ddlUpdateValue.SelectedValue)
            sql.AppendFormat(",ItemName='{0}' ", txtUpdateItemName.Text)
            sql.AppendFormat("where ID='{0}' ", key("ID"))

            dat.AccData(sql.ToString())

            Me.gv.EditIndex = -1
            Me.BindGV()
        ElseIf e.CommandName = "ItemDelete" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            sql.AppendFormat("delete from EcolifeItemMapping ")
            sql.AppendFormat("where ID='{0}' ", key("ID"))

            dat.AccData(sql.ToString())
            Me.BindGV()
        End If

    End Sub

    Protected Sub ddlUpdateGroup_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvRow As GridViewRow = TryCast(TryCast(sender, DropDownList).Parent.Parent, GridViewRow)
        Dim ddlUpdateGroup As DropDownList = gvRow.FindControl("ddlUpdateGroup")
        Dim ddlUpdateTableName As DropDownList = gvRow.FindControl("ddlUpdateTableName")
        Dim ddlUpdateValue As DropDownList = gvRow.FindControl("ddlUpdateValue")

        Me.BindDDLTableName(ddlUpdateTableName, ddlUpdateGroup.SelectedValue)
        Me.BindDDLValue(ddlUpdateValue, ddlUpdateTableName.SelectedValue)
    End Sub

    Protected Sub ddlUpdateTableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvRow As GridViewRow = TryCast(TryCast(sender, DropDownList).Parent.Parent, GridViewRow)
        Dim ddlUpdateTableName As DropDownList = gvRow.FindControl("ddlUpdateTableName")
        Dim ddlUpdateValue As DropDownList = gvRow.FindControl("ddlUpdateValue")

        Me.BindDDLValue(ddlUpdateValue, ddlUpdateTableName.SelectedValue)
    End Sub
#End Region

#Region "繫結資料"
    '群組
    Private Sub BindDDLGroup(ByVal ddlItem As DropDownList)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select ColumnText as Text,ItemCodeHeaderID as value from CodeItem where ")
        sql.AppendFormat("(TableName='FindDirtyItem' and ColumnName='DirtyItemGroup') ")
        sql.AppendFormat("or  ")
        sql.AppendFormat("(TableName='WorkItem' and ColumnName='ItemGroup') ")

        ddlItem.DataTextField = "Text"
        ddlItem.DataValueField = "value"
        ddlItem.DataSource = dat.GetData(sql.ToString())
        ddlItem.DataBind()

        ddlItem.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    '選項類別
    Private Sub BindDDLTableName(ByVal ddlItem As DropDownList, ByVal GroupValue As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select ColumnText as Text,TableName as value from CodeItem where ")
        sql.AppendFormat("CodeHeaderID='{0}' ", GroupValue)

        ddlItem.DataTextField = "Text"
        ddlItem.DataValueField = "value"
        ddlItem.DataSource = dat.GetData(sql.ToString())
        ddlItem.DataBind()

        ddlItem.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    '選項名稱
    Private Sub BindDDLValue(ByVal ddlItem As DropDownList, ByVal TableNameValue As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select * from codemapping where 1=1 ")
        sql.AppendFormat("and TableName='{0}' ", TableNameValue)
        sql.AppendFormat("and ColumnName='{0}' ", "Itemname")
        sql.AppendFormat("and Active='{0}' ", "Y")

        ddlItem.DataTextField = "Text"
        ddlItem.DataValueField = "value"
        ddlItem.DataSource = dat.GetData(sql.ToString())
        ddlItem.DataBind()

        ddlItem.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select a.ID,a.ItemName ")
        sql.AppendFormat(",a.Value ")
        sql.AppendFormat(",b.Text as Value_Text ")
        sql.AppendFormat(",a.TableName ")
        sql.AppendFormat(",c.ColumnText as TableName_Text ")
        sql.AppendFormat(",c.CodeHeaderID as Group_ID ")
        sql.AppendFormat(",d.ColumnText as Group_Text ")
        sql.AppendFormat("from EcolifeItemMapping a  ")
        sql.AppendFormat("inner join codemapping b ")
        sql.AppendFormat("	on a.TableName=b.TableName ")
        sql.AppendFormat("	and b.ColumnName='Itemname' ")
        sql.AppendFormat("	and a.Value=b.Value ")
        sql.AppendFormat("	and b.Active='Y' ")
        sql.AppendFormat("inner join CodeItem c  ")
        sql.AppendFormat("on b.TableName=c.TableName ")
        sql.AppendFormat("inner join CodeItem d ")
        sql.AppendFormat("on c.CodeHeaderID=d.ItemCodeHeaderID ")
        sql.AppendFormat("where 1=1 ")
        If Me.ddlGroup.SelectedValue <> "" Then
            sql.AppendFormat("and c.CodeHeaderID='{0}' ", Me.ddlGroup.SelectedValue)
        End If

        If Me.ddlTableName.SelectedValue <> "" Then
            sql.AppendFormat("and a.TableName='{0}' ", Me.ddlTableName.SelectedValue)
        End If

        If Me.ddlValue.SelectedValue <> "" Then
            sql.AppendFormat("and a.Value='{0}' ", Me.ddlValue.SelectedValue)
        End If

        If Me.gv.OrderBySQL = "" Then
            sql.AppendFormat("order by d.ColumnText,c.TableName_Text,b.Text ")
        Else
            sql.AppendFormat(Me.gv.OrderBySQL)
        End If
        gv.DataSource = dat.GetData(sql.ToString())
        gv.DataBind()

    End Sub
#End Region

End Class
